function my_clock(el){
    var today=new Date();
    var h=today.getHours();
    var m=today.getMinutes();
    var s=today.getSeconds();
    m=m>=10?m:('0'+m);
    s=s>=10?s:('0'+s);
    el.innerHTML = h+":"+m+":"+s;
    setTimeout(function(){my_clock(el)}, 1000);
}

var clock_div = document.getElementById('clock_div');
my_clock(clock_div);

//getChildren方法可以返回以指定节点为父节点的下一级书签节点，但不包括再下一级的节点
//排除
// chrome.bookmarks.getChildren('0', function(bookmarkArray){
//   console.log(bookmarkArray);
//   console.log(bookmarkArray.forEach(element => {
//     console.log(element)
//   }))
//   $('#tree_div_1').treeview({data: bookmarkArray});
// });
console.log("----------------")
//封装书签
function getBookMark(obj){
  var bookMark = {
    dateAdded:obj.dateAdded,
    id:obj.id,
    index:obj.index,
    parentId:obj.parentId,
    title:obj.title,
    url:obj.url,
    text: obj.title
  };
  //console.log("封装书签方法内部打印");
  //console.log(bookMark);
  return bookMark;
}
//封装目录 保存目录信息，向下递归
function getCatalogue(obj){
  var catalogue = {
    children:[],
    dateAdded:obj.dateAdded,
    dateGroupModified:obj.dateGroupModified,
    id:obj.id,
    index:obj.index,
    parentId:obj.parentId,
    title:obj.title,
    text: obj.title,
    nodes: []
  }
  var cars = [];
  var childArray = obj.children;
  for(var i=0;i<childArray.length;i++){
    //判断是不是文件夹
    if('children' in childArray[i]){
      //是文件夹，保存目录信息，然后接着向下索引
      cars.push(getCatalogue(childArray[i]));
    }else{
      //不是文件夹，直接封装为对象
      cars.push(getBookMark(childArray[i]));
    }
  }
  catalogue.children=cars;
  catalogue.nodes=cars;
  //console.log("封装目录方法内部打印");
  //console.log(catalogue);
  return catalogue;
}
//递归算法 传入节点object,判断是不是目录，不是目录就添加到数组，是目录就递归查询再添加到数组
function digui(nodeObj){
  var cars = [];
  //判断是不是文件夹
  if('children' in nodeObj){
    cars.push(getCatalogue(nodeObj));
  }else{
      //不是文件夹，直接封装为对象
      cars.push(getBookMark(nodeObj));
  }
  return cars;
}



//通过getTree方法可以获得用户完整的书签树
chrome.bookmarks.getTree(function(bookmarkArray){
  // console.log(bookmarkArray);
  // console.log(bookmarkArray[0]);
  // console.log(bookmarkArray[0].children);
  // console.log(bookmarkArray[0].children.length);

  //var wenjianjia =bookmarkArray[0].children;
  console.log(bookmarkArray);
  var jieguo = digui(bookmarkArray[0]);
  $('#tree_div_1').treeview({data: jieguo});
});

//onCreated事件用以监控书签的创建行为：
chrome.bookmarks.onCreated.addListener(function(bookmark){
  console.log("书签创建")
  console.log(bookmark);
});

//onRemoved事件用以监控书签的移除行为：
chrome.bookmarks.onRemoved.addListener(function(id, removeInfo){
  console.log("书签移除")
  console.log('Bookmark '+id+' has been removed:');
  console.log(removeInfo);
});

//onChanged事件用以监控书签的更新行为：
chrome.bookmarks.onChanged.addListener(function(id, changeInfo){
  console.log('Bookmark '+id+' has been changed:');
  console.log(changeInfo);
});
//onMoved事件用以监控书签的移动行为：
chrome.bookmarks.onMoved.addListener(function(id, moveInfo){
  console.log('Bookmark '+id+' has been moved:');
  console.log(moveInfo);
});
//onChildrenReordered事件用以监控一个书签分组下的更改子节点顺序的行为：
chrome.bookmarks.onChildrenReordered.addListener(function(id, reorderInfo){
  console.log('Bookmark '+id+' has a new children order:');
  console.log(reorderInfo);
});
//onImportBegan和onImportEnded事件分别用以监控导入书签开始和结束的行为：
onImportBegan(function(){
  console.log('Bookmark import began.');
});

onImportEnded(function(){
  console.log('Bookmark import ended.');
});
















//https://www.npmjs.com/package/bootstrap-treeview
//https://gitee.com/mirrors/Bootstrap-Tree-View#https://gitee.com/link?target=http%3A%2F%2Fgetbootstrap.com%2F
//https://www.cnblogs.com/zx-admin/p/7872046.html